package com.cl.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.entity.XueshengEntity;
import com.cl.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cl.entity.LaoshiEntity;
import com.cl.service.LaoshiService;
import com.cl.utils.PageUtils;
import com.cl.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 教师
 */
@RestController
@RequestMapping("/laoshi")
public class LaoshiController {
    @Autowired
    private LaoshiService laoshiService;

    @Autowired
    private TokenService tokenService;
    /**
     * 登录
     */
    @IgnoreAuth
    @RequestMapping(value = "/login")
    public R login(String username, String password, String captcha, HttpServletRequest request) {
        LaoshiEntity u = laoshiService.selectOne(new EntityWrapper<LaoshiEntity>().eq("gonghao", username));
        if(u==null || !u.getMima().equals(password)) {
            return R.error("账号或密码不正确");
        }
        String token = tokenService.generateToken(u.getId(), username,"laoshi",  "老师" );
        return R.ok().put("token", token);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = laoshiService.queryPage(params);
        return R.ok().put("page", page);
    }
    
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        LaoshiEntity laoshi = laoshiService.selectById(id);
        return R.ok().put("data", laoshi);
    }
    
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LaoshiEntity laoshi){
        laoshiService.insert(laoshi);
        return R.ok();
    }
    
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LaoshiEntity laoshi){
        laoshiService.updateById(laoshi);
        return R.ok();
    }
    
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        laoshiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
} 